package main.java.study;

import org.springframework.util.ObjectUtils;

import javax.xml.crypto.Data;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Optional;

import static java.time.temporal.TemporalAdjusters.*;

/**
 * @Author 刘伟涛
 * @Date 2023/7/25 10:49
 **/
public class 时间格式转换 {
    public static void main(String[] args) throws ParseException {
        // 1
        study.User user = new study.User();
        System.out.println(ObjectUtils.isEmpty(user.getId()) ? null : new Date(user.getId()));
        System.out.println(System.currentTimeMillis());
        // long类型转Date类型
        long currentTimeMillis = System.currentTimeMillis();
        Date data = new Date(1699583534968L);

        // Date类型转String类型，指定年月日时分秒
        System.out.println("data:" + data);
        String format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(data);
        /*        String time = "1699583534968";
        Date parse = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time);
        System.out.println("parse:" + parse);*/
        // 3
        System.out.println("format1:" + format1);

        // 将 long 转换为 LocalDateTime
        LocalDateTime localDateTime =
            LocalDateTime.ofInstant(Instant.ofEpochMilli(currentTimeMillis), ZoneId.systemDefault());

        // Date类型转LocalDateTime类型
        LocalDateTime localDateTime2 = LocalDateTime.ofInstant(data.toInstant(), ZoneId.systemDefault());
        System.out.println(localDateTime); // 输出格式: 2023-10-05T16:32:14.968

        // LocalDateTime类型转Date类型
        Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
        System.out.println(date); // 输出格式: Fri Oct 05 16:32:14 CST 2023
        LocalDateTime now = LocalDateTime.now();
        // 4
        System.out.println(now.toString());
        // LocalDateTime类型转String类型，指定年月日时分秒
        String format = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        // LocalDateTime parse = LocalDateTime.parse(format);
        System.out.println(format);
        System.out.println(new Date());
        //
        Timestamp timestamp = Timestamp.valueOf(format);
        System.out.println("timestamp" + timestamp);
        // ------------------------------------
        LocalDate now1 = LocalDate.now();
        now1 = now1.with(nextOrSame(DayOfWeek.SUNDAY));
        LocalDate monthLastDay = now1.with(lastDayOfMonth());
        System.out.println(monthLastDay);


        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
        String formattedDate = now.format(formatter);
        // 输出结果
        System.out.println("当前日期（年月日格式）：" + formattedDate);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        String formattedDate1 = sdf.format(data);
        System.out.println(formattedDate1); // 输出格式: 2023年10月05日（根据当前日期）

    }
}
